SlicerTMS: Real-Time Visualization of Transcranial Magnetic Stimulation for Mental Health Treatment

We present a real-time visualization system for Transcranial Magnetic Stimulation (TMS), a non-invasive neuromodulation technique for treating various brain disorders and mental health diseases. Our solution targets the current challenges of slow and labor-intensive practices in treatment planning. Integrating Deep Learning (DL), our system rapidly predicts electric field (E-field) distributions in 0.2 seconds for precise and effective brain stimulation. The core advancement lies in our tool’s real-time neuronavigation visualization capabilities, which support clinicians in making more informed decisions quickly and effectively. We assess our system’s performance through three studies: First, a real-world use case scenario in a clinical setting, providing concrete feedback on applicability and usability in a practical environment. Second, a comparative analysis with another TMS tool focusing on computational efficiency across various hardware platforms. Lastly, we conducted an expert user study to measure usability and influence in optimizing TMS treatment planning. The system is openly available for community use and further development on GitHub: https://github.com/lorifranke/SlicerTMS.


Introduction
Transcranial Magnetic Stimulation (TMS) [4] is a non-invasive method to treat brain disorders such as depression, migraines, and addictions, and it is used in research for Parkinson's, Schizophrenia, and Alzheimer's disease.The technique uses a device called TMS coil to stimulate brain neurons by generating electromagnetic fields (E-Field).Precise coil placement for targeted therapy is crucial for treatment outcomes [3,5].Despite its wide clinical application [14,6,2], traditional E-field estimation methods involve complex head models and computationally demanding techniques, which are not practical for real-time clinical use due to their dependence on high-performance graphics processing units (GPUs) [25,11].This complexity leads to lengthy preparation times, including many hours of manual setup and automated model construction [30].The need for real-time visualization for TMS was already identified in 2004 [15] but poses challenges for real-time prediction and visualization in clinical settings.
In this paper, we present SlicerTMS, the first open-source software enabling real-time E-field prediction and visualization for TMS treatment, using deep learning (DL) within a neuronavigation system for immediate visualization.This innovation allows clinicians to adjust the TMS coil on a patient's head in realtime, with the DL model providing instant E-field updates, a significant advancement over traditional TMS tools that offer only static visualizations [24,1,21].In clinical settings, the demand for real-time visualization and AI for enhanced clinical planning and medical imaging analysis is growing, yet there's a gap between AI advancements and their application in healthcare.Besides real-time visualizations, traditional TMS tools often lack deep learning and integration with neuronavigation [12,22,20].Furthermore, SlicerTMS utilizes web-based AR for enhanced interaction and placement of TMS coils and MRI images, benefiting from the advancement of XR technologies in medical applications [27].We enhance the system's usability and accuracy by incorporating 3D Slicer [8], a widely used open-source visualization platform for medical imaging and optical tracking.Our integration accelerates E-field estimation and leverages the robust capabilities of 3D Slicer, which various tools have extended in recent years [17,19,23,18].We evaluate SlicerTMS's usability and functionality through experiments on various hardware and data from ten patients, comparing it with a current TMS tool to demonstrate speed and usability advantages.A real-world use case in a TMS clinic shows the system's practicality, complemented by feedback from a domain expert study.Involving medical experts for user-centered design is critical to bridge this gap and effectively integrate and optimize AI technologies in clinics as real-world applications.SlicerTMS enhances TMS visualization and medical visualization by providing a novel, open-source platform for real-time E-field visualization and deep learning, facilitating efficient and precise clinical brain stimulation planning.

Neuronavigation Visualization Component
The final interface of SlicerTMS (Figure 1) integrates a client-server architecture with neural networks for electric field predictions, a primary user interface, and augmented reality visualization.SlicerTMS provides real-time rendering of predicted E-Field on different modalities: a 3D brain mesh, its 3D volume, and the brain's white matter fiber bundles obtained from MRI scans for patient-specific modeling.Our tool is integrated into the neuronavigation software 3D Slicer [8], with Kitware's VTK framework to manipulate graphical elements.Users can visualize and explore real-time TMS results from standard desktop monitors on different operating systems for easy navigation with adjustable visualization parameters for a thorough analysis of different scenarios.Data is transmitted from the neural network through a protocol called OpenIGTLink, a tool for network communication with external software or hardware using a protocol allowing real-time image and position streaming with submillisecond latency up to 1024 fps [28], for performance and scalability criteria.The neural network server component can run locally or as a remote service on a GPU server with data transfer via OpenIGTLink and facilitate smooth performance during computationally intensive tasks.Each time the neural network generates a new E-Field, it updates the TMS module and visualization.Inside SlicerTMS' user interface, users can manually adjust the coil by dragging and rotating it with the cursor into the desired position or using text input fields to enter a coil position matrix where the 3D coil automatically jumps into position.While moving the coil, we inform the neural network via OpenIGTLink, generating a new E-Field for this specific coil position (see Figure 1).We created a coil as a 3D mesh in .stlformat, simulating a 'figure 8' coil like the Magstim-70mm-Figure8, a standard TMS treatment coil.Users can freely use any other type of TMS coil by exchanging the example coil with their coil file in the data folder.Equally, all other files, such as skin, gray matter, and conductivity files, can be exchanged individually, allowing for patient-specific treatment.2D and 3D views of the neuronavigation component are illustrated in Figure 2.

Deep Learning Pipeline for E-Field Prediction
Several deep-learning-based approaches have been developed for estimating brain E-Fields [32,31,13,26,7].While [32] did not consider subject-specific brain connectivity, methods of [31] and [13] use subject-specific whole-brain E-field prediction.We train a multi-scale 3D-Res-UNet model with a reduced field-of-view to accelerate prediction time.We use T 1w MRI images and diffusion MRI from the Human Connectome Project (HCP) [29] for training.Using SimNIBS [21], we constructed volume conductor models from T 1w images and estimated anisotropic tissue conductivity tensors from diffusion MRI of b = 0 and 1000s/mm 2 volumes.We varied the coil's position and orientation for diverse E-field maps, aligning with EEG 10-10 system positions and adjusting the coil handle to 78 different directions.We randomly selected a total of 300 cases from this dataset.Models were trained on NVIDIA V100 32GB GPU with PyTorch for 2000 epochs until convergence with Adam optimizer and a tailored learning rate strategy.Input volumes for the models comprised concatenated data of conductivity tensors and time derivative of the magnetic potential, formatted as three-dimensional arrays to preserve spatial information crucial for E-field prediction.Performance evaluations were based on normalized root-mean-square error (NE).Our model has comparable accuracy reported by [31], achieving a NE of 0.198 ± 0.017.

AR Component with WebXR
SlicerTMS features a web server for connection to local browsers, using Tornado for secure WebSocket communication to enable real-time interaction with the 3D Slicer neuronavigation platform.Users can control the coil by moving or rotating their mobile phone, which has a depth sensor, with position updates sent via WebSocket.The system uses JavaScript with ThreeJS for client-side rendering and the WebXR API [9] supporting mobile-based AR and VR headsets, offering three coil interaction options (see Figure 1 Supplemental Material).

Real-world Use Case Scenario
For further enhancements, we tested our prototype in a Boston area clinic.This out-of-lab study involved six participants, including clinicians, a subject undergoing an MRI scan, and researchers, to simulate TMS treatment using an optical tracker system for coil placement.Collaborating with experts, we faced challenges aligning brain images with the patient's head using the optical tracker.We must ensure a reliable internet connection to run our deep learning model.This hands-on testing provided valuable real-world insights into the usability and integration of SlicerTMS in clinical settings.We included these improvements into our prototype for efficient TMS clinic workflow, including UI improvements for manual coil position entry, brain fibers visualization from diffusion MRI data for clinical and patient-specific treatment relevance [16,10], color map updates for electric field distributions, mobile device-enabled AR feature for setup simplification, and electric field visualization on volume data to aid radiological studies, as in Figure 2. The updates aim to refine SlicerTMS for smoother, more effective TMS treatment planning.

Technical Performance Experiments of SlicerTMS
For experiments, we randomly selected ten different subjects from the Human Connectome Project (HCP ) dataset [29] consisting of so-called T1 w MRIs.T1 w MPRAGE images were acquired with 0.7mm isotropic voxels.We replaced each subject's conductivity file, skin, and brain mesh while the coil and pre-trained deep learning model remained the same throughout all subjects.We evaluated SlicerTMS on four devices for real-time visualization with deep learning and compared it to a leading TMS visualization tool in Section 4.2.We evaluated based on E-field prediction (CNN) and real-time visualization (Vis).Results can be seen in Table 1.Input data consisted of an electric field of shape 70x90x50x1 generated by our trained model.The visualization includes resampling Nifti images and simultaneously projecting the E-field on a 3D brain mesh, brain volume, and tractography data.We registered how long the code needs to execute each run, where a run is defined as the coil movement triggering the neural network, resulting in predicting an E-field that is immediately visualized in SlicerTMS.To get a precise measurement, we averaged the time of 50 runs on following devices: an Apple M1 MacBookBook Air with 16GB Memory (Apple M1), a workstation computer with an Intel Core i9-9980XE with 36 CPUs @ 3.00GHz and 64 GB RAM (CPU i9), an NVIDIA GeForce RTX 2080 GPU (2080Ti), and a remote NVIDIA A100 GPU (A100).Results of our performance testing indicate that our neural network runs on average in less than 0.2 seconds and real-time visualization in less than ten milliseconds.

Comparative Performance Analysis
We compared SlicerTMS to the existing TMS visualization software SimNIBS [21].Unlike SlicerTMS, SimNIBS does not rely on deep learning or real-time but solely on statically visualizing E-fields based on manual coil placement.We tested both tools on ten subjects from the HCP dataset.We used SimNIBS v4.0 to compute E-fields induced by a Magstim-70mm-Figure8 coil at various locations and orientations.We measured the time only for the visualization part inside SimNIBS.We created the E-field for each of the ten subjects in random positions and entered these exact coil position matrices in SlicerTMS to generate the same Efields.Figure 4 shows the same coil positions in both tools.We did not measure additional times SimNIBS requires, i.e., calculating the dA/dt field, computing matrices, or solving the system.Table 2 shows the results of visualization speed comparison on two CPU machines.SlicerTMS took, on average, 0.08506 seconds, while SimNIBS needs 7.58798 seconds to visualize an E-field on the brain.We conducted a two-sided t-test to determine significant differences in group means.The null hypothesis states no significant difference between SlicerTMS and SimNIBS (H 0 ), while the alternative hypothesis states a significant difference between groups (H 1 ).We used an independent samples t-test as the two groups are independent.The t-statistic is t 38 = 56.3,p < 0.0001, indicating a

Expert User Study
We conducted an expert user study to evaluate the impact of real-time TMS E-Field visualization on usability, workflow efficiency, and clinician trust in DL predictions, involving four TMS experts in tasks that simulate real-world scenarios.Feedback and a post-study questionnaire, alongside a NASA-TLX survey, were used to refine SlicerTMS, assessing its usability, utility, and workload in clinical settings.The study questionnaire can be found in the supplemental material.The study results highlight the effectiveness of SlicerTMS in facilitating the interpretation of TMS electric fields, with experts successfully identifying brain regions and electric field strengths.Feedback indicates that SlicerTMS was appreciated for its functionality and ease of use, with areas identified for enhancement in 2D slice interactions.Participants noted SlicerTMS could improve TMS treatment planning and research by enabling real-time electric field visualization, a clear improvement over previous static images, and voiced interest in its use in clinical settings, along with suggestions for further improvements.

Discussion
SlicerTMS enhances brain stimulation treatment planning with real-time visual feedback.It is faster than previous methods, achieving near-smooth frame rates with remote GPUs.The system's dynamic visualizations improve the intuitive understanding of electric fields.TMS experts believe it could speed up treatment planning but suggest improving coil manipulation and 2D interactions.While acknowledging SlicerTMS's advancements, we also recognize the limitations compared to tools like SimNIBS, aiming to incorporate additional functionalities to bridge these gaps in future developments.

Conclusion and Future Work
This paper presents SlicerTMS, an innovative AI system for real-time Transcranial Magnetic Stimulation visualization.It enhances coil placement through integration with 3D Slicer and augmented reality.Its effectiveness, validated in a TMS clinic and through professional feedback, highlights its potential to upgrade treatment planning with high-speed, flexible visualizations.Leveraging both local and remote GPUs, SlicerTMS advances medical visualization significantly.Future improvements will measure coil-cortex distance, electric field direction visualization, and enhanced augmented reality brain projections.

Fig. 1
Fig. 1. a) Components: Neural Network (left) predicts E-field and transfers it to SlicerTMS via OpenIGTLinkIF.WebSocket supports browser connection to WebXR to interact with visualization in AR (right).b) Neuronavigation Visualization: Incoming magnetic vector field images are transformed according to coil position, then overlayed with the brain mesh.Consistent rotation of vector direction in each voxel as rigid transform is critical.The 3D coil can be moved interactively while sending new coil positioning matrices back to neural network generating a new field.

Fig. 2 .
Fig. 2. Gallery of example visualizations: Brain Mesh vs. Volume Rendering vs. Fiber Tractography in SlicerTMS.E-field in 3D on gray matter with a figure-8 coil (Left), E-field on MRI volumetric data (Center), and E-field on full-brain tractography fibers with adjustable ROI and 2D slices in various directions (Right).

Fig. 3 .
Fig. 3. TMS Clinic and Output in SlicerTMS.Researcher administering treatment by standing behind patient and adjusting the TMS coil to target the right brain areas, meanwhile SlicerTMS is running in with a remote connection to server to predict the electric field in real-time on the brain mesh shown in the UI.

Fig. 4 .
Fig. 4. SlicerTMS vs. SimNIBS E-Field Visualization.Left: SlicerTMS E-field with movable Figure 8 coil, color legend indicates strength of E-field and axes with directions.Right: SimNIBS E-field on brain surface with coil direction (green handle).

Table 1 .
Performance Evaluation.Timings for E-field prediction (CNN) and visualization (Vis.) are shown separately for ten different subjects using four different hardware configurations.The remote A100 setup using cloud-based inference is fastest.All times in seconds.